﻿
using CSRedis;

namespace AdminSystem.WebApi
{
    public class BlogBackgroundService : BackgroundService
    {
        private readonly ILogger<BlogBackgroundService> logger;
        private readonly IConfiguration configuration;

        public BlogBackgroundService(ILogger<BlogBackgroundService> logger, IConfiguration configuration)
        {
            this.logger = logger;
            this.configuration = configuration;
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            CSRedisClient cSRedisClient = new CSRedisClient(configuration.GetConnectionString("Redis"));

            /*Thread thread = new Thread(async () => {
                while (true)
                {
                    var blog = await cSRedisClient.RPopAsync("BlogController");

                    logger.LogInformation("++++++++++++++++++");
                    logger.LogInformation(blog);

                    for (int i = 0; i < 3; i++)
                    {
                        //模拟并发
                        Thread.Sleep(1000);
                    }

                    //Console.WriteLine(DateTime.Now);

                    Thread.Sleep(1000);
                }
            });

            thread.Start();*/

            cSRedisClient.Subscribe(("channel.1", msg => {
                logger.LogInformation("++++++++++++++++++");
                logger.LogInformation(msg.Body);
                }
            ));

            return Task.CompletedTask;
        }
    }
}
